package com.example.petshopping.ui.bidding;

import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.petshopping.R;
import com.example.petshopping.ui.bidding.adapter.BiddingProductAdapter;
import com.example.petshopping.ui.bidding.model.BiddingProduct;

import java.util.ArrayList;
import java.util.List;

public class BiddingViewActivity extends AppCompatActivity implements BiddingProductAdapter.OnBidClickListener {

    private TextView tvTodayBidding;
    private TextView tvAllBidding;
    private TextView tvBadgeCount;
    private CheckBox cbMyBids;
    private RecyclerView rvProducts;
    private BiddingProductAdapter adapter;
    private List<BiddingProduct> productList = new ArrayList<>();
    private View historySection;
    private boolean isHistoryExpanded = false;
    private ImageView historyIcon;
    private ImageView btnBack;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bidding_view);
        
        initViews();
        setupListeners();
        loadProducts();
        setupRecyclerView();
    }
    
    private void initViews() {
        tvTodayBidding = findViewById(R.id.tv_today_bidding);
        tvAllBidding = findViewById(R.id.tv_all_bidding);
        tvBadgeCount = findViewById(R.id.tv_badge_count);
        cbMyBids = findViewById(R.id.cb_my_bids);
        rvProducts = findViewById(R.id.rv_products);
        historySection = findViewById(R.id.history_section);
        historyIcon = findViewById(R.id.history_icon);
        btnBack = findViewById(R.id.btn_back);
        
        // 默认选中"全部参与"选项卡
        tvAllBidding.setSelected(true);
        
        // 设置徽章数量
        tvBadgeCount.setText("2");
    }
    
    private void setupListeners() {
        // 设置返回按钮点击事件
        btnBack.setOnClickListener(v -> finish());
        
        // 设置选项卡点击事件
        tvTodayBidding.setOnClickListener(v -> {
            tvTodayBidding.setSelected(true);
            tvAllBidding.setSelected(false);
            // 根据选项卡更新商品列表
            filterProductsByTab("today");
            Toast.makeText(this, "切换到今晚截拍", Toast.LENGTH_SHORT).show();
        });
        
        tvAllBidding.setOnClickListener(v -> {
            tvAllBidding.setSelected(true);
            tvTodayBidding.setSelected(false);
            // 根据选项卡更新商品列表
            filterProductsByTab("all");
            Toast.makeText(this, "切换到全部参与", Toast.LENGTH_SHORT).show();
        });
        
        // 设置"我的出价"复选框点击事件
        cbMyBids.setOnCheckedChangeListener((buttonView, isChecked) -> {
            // 根据复选框更新商品列表
            filterProductsByMyBids(isChecked);
        });
        
        // 设置历史记录点击事件
        historySection.setOnClickListener(v -> {
            isHistoryExpanded = !isHistoryExpanded;
            updateHistorySectionUI();
            filterCompletedProducts();
        });
    }
    
    private void loadProducts() {
        // 添加进行中的商品
        BiddingProduct product1 = new BiddingProduct();
        product1.setId(1);
        product1.setTitle("宠物标题宠物标题宠物标题宠物标题...");
        product1.setImageResource(R.drawable.yugang);
        product1.setPrice("432");
        product1.setAuctionTime("今晚21:22截拍");
        product1.setBidCount(3);
        product1.setViewerCount(21);
        product1.setLeading(true);
        product1.setNoReason(true);
        product1.setCompleted(false);
        product1.setMyBid(true);
        product1.setTodayEnding(true);
        productList.add(product1);

        BiddingProduct product2 = new BiddingProduct();
        product2.setId(2);
        product2.setTitle("宠物标题宠物标题宠物标题宠物标题...");
        product2.setImageResource(R.drawable.yugang);
        product2.setPrice("432");
        product2.setAuctionTime("今晚21:22截拍");
        product2.setBidCount(3);
        product2.setViewerCount(21);
        product2.setLeading(true);
        product2.setNoReason(true);
        product2.setCompleted(false);
        product2.setMyBid(true);
        product2.setTodayEnding(true);
        productList.add(product2);

        // 添加已截拍商品
        BiddingProduct product3 = new BiddingProduct();
        product3.setId(3);
        product3.setTitle("宠物标题宠物标题宠物标题宠物标题...");
        product3.setImageResource(R.drawable.yugang);
        product3.setPrice("432");
        product3.setAuctionTime("07-02已截拍");
        product3.setBidCount(3);
        product3.setViewerCount(21);
        product3.setLeading(false);
        product3.setNoReason(true);
        product3.setCompleted(true);
        product3.setWatchOnly(true);
        product3.setMyBid(false);
        product3.setTodayEnding(false);
        productList.add(product3);
    }
    
    private void setupRecyclerView() {
        adapter = new BiddingProductAdapter(this, productList, this);
        rvProducts.setLayoutManager(new LinearLayoutManager(this));
        rvProducts.setAdapter(adapter);
        
        // 默认隐藏已截拍商品
        filterCompletedProducts();
    }
    
    private void updateHistorySectionUI() {
        // 根据展开/折叠状态更新历史记录图标
        if (isHistoryExpanded) {
            historyIcon.setImageResource(R.drawable.top); // 使用向上箭头图标
            historyIcon.setRotation(180); // 旋转180度
        } else {
            historyIcon.setImageResource(R.drawable.top); // 使用向下箭头图标
            historyIcon.setRotation(0); // 重置旋转
        }
    }
    
    private void filterProductsByTab(String tabType) {
        if (adapter != null) {
            adapter.filterByTab(tabType);
        }
    }
    
    private void filterProductsByMyBids(boolean showOnlyMyBids) {
        if (adapter != null) {
            adapter.filterByMyBids(showOnlyMyBids);
        }
    }
    
    private void filterCompletedProducts() {
        if (adapter != null) {
            adapter.filterByCompletedStatus(isHistoryExpanded);
        }
    }
    
    @Override
    public void onBidClick(BiddingProduct product) {
        if (product.isCompleted()) {
            Toast.makeText(this, "该商品已截拍", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(this, "准备出价: ¥" + product.getPrice(), Toast.LENGTH_SHORT).show();
            // 这里可以跳转到出价页面或弹出出价对话框
        }
    }
} 